package com.zoshine.zzg.service;

import com.zoshine.zzg.dao.InvoiceDao;
import com.zoshine.zzg.dao.SettlementDao;
import com.zoshine.zzg.pojo.Invoice;
import com.zoshine.zzg.pojo.Settlement;
import com.zoshine.zzg.pojo.pojostatus.OrderStatus;
import com.zoshine.zzg.pojo.pojostatus.SettlementStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 田沛凌
 * @time 2017/7/25 9:57
 */
@Service
@Transactional
public class SettlementService extends BaseService<Settlement> {
    private SettlementDao settlementDao;
    private InvoiceDao invoiceDao;

    @Autowired
    public void setSettlementDao(SettlementDao settlementDao) {
        this.settlementDao = settlementDao;
    }

    @Autowired
    public void setInvoiceDao(InvoiceDao invoiceDao) {
        this.invoiceDao = invoiceDao;
    }

    /**
     * 生成结算单
     * @param idStr 发货单ID集合
     */
    public void addSettlement(String idStr) {

        String[] pidArr = idStr.split(",");

        //考虑勾选多条发货单有不同的供应商，所以根据供应商来生成结算单
        Set<Long> supplierIds= new HashSet<>();
        List<Invoice> invoices=new ArrayList<>();
        for (String id : pidArr) {
            Invoice invoice = invoiceDao.selectByPrimaryKey(Long.parseLong(id));
            invoices.add(invoice);
            supplierIds.add(invoice.getAccountSupplier());
        }
        for (Long id:supplierIds) {
            Settlement settlement=new Settlement();
            settlement.setId(baseIdWorker.createId());
            Invoice temp=null;
            boolean flg=true;
            //计算总金额
            Integer total = 0;
            for (Invoice invoice:invoices){
                //表示是同一个供应商下的发货单
                if(id.equals(invoice.getAccountSupplier())){
                    total+=invoice.getTotalSettlement();
                    invoice.setStatus(OrderStatus.FINISHED.getValue());
                    invoice.setSettlementId(settlement.getId());
                    invoiceDao.update(invoice);
                    if(flg){
                        temp=invoice;
                        flg=false;
                    }
                }
            }
            settlement.setTotalSettlement(total);
            settlement.setAccountPrisons(temp.getAccountPrisons());
            settlement.setAccountSupplier(id);
            settlement.setStatus(SettlementStatus.PENDING_RECEIVE.getValue());
            settlement.setCreateTime(new Date());
            settlement.setUpdateTime(new Date());
            settlementDao.insert(settlement);
        }
    }

    /**
     * 查询结算列表集合
     * @param settlement 结算单
     * @return 结算集合
     */
    public List<Settlement> settlementList (Settlement settlement){
        List<Settlement> settlements = settlementDao.selectList(settlement);
        return  settlements;
    }

    /**
     * 状态改变接口
     * @param ids；
     * @param status；
     */
    public void changeSettlementStatus(List<Long> ids, Integer status) {
        settlementDao.batchUpdateStatus(status,ids);
    }
}